package cn.net.wanji.controller;

import cn.net.wanji.domain.AjaxResult;

import cn.net.wanji.entity.AntennaInfo;
import cn.net.wanji.entity.deviceInfo.DeviceInfo;
import cn.net.wanji.entity.deviceStatus.DeviceStatus;
import cn.net.wanji.entity.deviceStatus.WeatherStatus;
import cn.net.wanji.mapper.DataReceiveMapper;
import cn.net.wanji.service.DataReceiveService;
import cn.net.wanji.utils.LogUtils;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import cn.net.wanji.config.ApplicationConfig;
import cn.net.wanji.entity.JsonRoot;
import cn.net.wanji.entity.PreData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;


/**
 * @author guodejun
 * @createTime 2022/10/28 10:18
 * @description
 */

//@Log4j2
@Slf4j(topic = "查询etc上传json数据【DataReceiveController】")
@RestController
@ControllerAdvice
@RequestMapping(value = "/receive")
public class DataReceiveController {

    @Resource
    private DataReceiveService dataReceiveService;
    @Resource
    DataReceiveMapper dataReceiveMapper;
    @Resource
    ApplicationConfig applicationConfig;

    // 牌识数据接口
    @PostMapping("/predata")
    public AjaxResult preDataReceive(@RequestBody PreData preData) {
        try {
            if (preData == null) {
                return AjaxResult.error("predata is null");
            }
            dataReceiveService.preDataReceiveToKafka(preData);
            return AjaxResult.success();
        } catch (Exception e) {
            log.error("preData: {}", JSON.toJSONString(preData));
            LogUtils.errorLog(e);
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/data")
    public AjaxResult dataReceive(@RequestBody JsonRoot jsonRoot) {
        try {
            if (jsonRoot == null || jsonRoot.getRecordId() == null) {
                return AjaxResult.error("jsonRoot is null");
            }
            dataReceiveService.dataReceive(jsonRoot);
            return AjaxResult.success();
        } catch (Exception e) {
            log.error("jsonRoot: {}", JSON.toJSONString(jsonRoot));
            LogUtils.errorLog(e);
            return AjaxResult.error(e.getMessage());
        }
    }


    private String convertToJsonString(JsonRoot jsonRoot) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(jsonRoot);
    }

    private String convertToDataString(PreData preData) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(preData);
    }

    private String convertToDeviceInfoDataString(Object preData) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(preData);
    }

    /**
     * 0:正拍
     * 1:尾拍
     * 2:侧拍
     * 3:车牌
     * 4:短视频
     * 5:合成图
     *
     * @param files
     * @param fileTypes
     * @param recordId
     * @return
     */
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("files") MultipartFile[] files, Integer[] fileTypes, String recordId,
                                 String detectTime) {
        String fileInfo = Arrays.toString(files);
//        log.info("uploadFile: {},{},{},{}", recordId, fileInfo, Arrays.toString(fileTypes),detectTime);
        try {
            if (files == null || files.length == 0) {
                return AjaxResult.error("files 为空");
            } else if (recordId == null || recordId.isEmpty()) {
                return AjaxResult.error("recordId 不能为空");
            } else if (fileTypes == null || fileTypes.length == 0) {
                return AjaxResult.error("fileTypes 不能为空");
            } else if (files.length != fileTypes.length) {
                return AjaxResult.error("files 与 fileTypes 长度不相同");
            }
            // 存储
            dataReceiveService.saveFile(files, fileTypes, recordId, detectTime);

            return AjaxResult.success();
        } catch (Exception e) {
            log.error("uploadFile: {},{},{},{}", recordId, fileInfo, Arrays.toString(fileTypes), detectTime);
            LogUtils.errorLog(e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 设备信息上传 频率：系统初始启动时以及每日两点
     *
     * @param deviceInfo
     * @return
     */
    @PostMapping("/deviceInfoData")
    public AjaxResult deviceInfoData(@RequestBody DeviceInfo deviceInfo) {
        try {
            if (deviceInfo == null || deviceInfo.getGantryId() == null) {
                return AjaxResult.error("deviceInfo is null");
            }
            return AjaxResult.success();
        } catch (Exception e) {
            log.error("deviceInfoData: {}", JSON.toJSONString(deviceInfo));
            LogUtils.errorLog(e);
            return AjaxResult.error();
        }
    }

    @PostMapping("/deviceStatusData")
    public AjaxResult deviceStatusData(@RequestBody DeviceStatus deviceStatus) {
        try {
            if (deviceStatus == null || deviceStatus.getGantryId() == null) {
                return AjaxResult.error("deviceStatus is null");
            }
            List<AntennaInfo> antennaInfoList = deviceStatus.getAntennaInfoList();
            dataReceiveService.addAntennaInfo(antennaInfoList);
            return AjaxResult.success();
        } catch (Exception e) {
            log.info("deviceStatusData: {}", JSON.toJSONString(deviceStatus));
            LogUtils.errorLog(e);
            return AjaxResult.error();
        }
    }

    @PostMapping("/weatherStatus")
    public AjaxResult weatherStatus(@RequestBody WeatherStatus weatherStatus) {
        try {
            log.info("天气数据==》{}", weatherStatus.toString());
            return AjaxResult.success();
        } catch (Exception e) {
            LogUtils.errorLog(e);
            return AjaxResult.error(e.getMessage());
        }
    }

}
